const fs = require('fs');//?文件夹操作

let server = function (app, db) {

    // const web_information=fs.readFileSync('./html/webinformation.html');//?本站信息

    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from admin_login where AdminCookie=\'' + request.cookies.DOM_Cookie_admin + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //?获取用户所有菜品信息
    app.post('/user_recipe', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                if (request.body.UserID) {

                    db.query('select * from recipe where UserID=' + request.body.UserID, function (err, data) {
                        if (err) response.send({ "code": -1, "data": "error" })

                        else if (data[0]) {
                            response.send({ "code": 0, "data": data })
                        }
                    })
                } else response.send({ "code": -1, "data": "error" })
            } else {
                response.send({ "code": -1, "data": "请先登录" })
            }
        })
    })

    //?删除菜谱
    app.post('/admin_delete', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('delete from recipe where FoodID=\'' + request.body.FoodID + '\'', function (err, data) {
                    if (err) {
                        console.error(err);
                        response.send({ "code": -1, "result": 'error' })
                    } else {
                        console.log(data);
                        response.send({ "code": 0, "result": "success" })
                    }
                })
            } else {
                response.send({ "code": -1, "data": "请先登录" })
            }

        })
    })

}
module.exports = server